"""
运行登录功能测试的脚本
"""
import os
import sys
import time
import logging
import pytest
from datetime import datetime

# 设置项目根目录到系统路径
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(ROOT_DIR)

# 配置日志
from tests.utilities.logger import setup_logger
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
logger = setup_logger()  # 保存logger引用

# 确保配置文件中的BASE_URL正确
import tests.config.config as config
# 设置BASE_URL为登录页面的基础URL
config.BASE_URL = "http://localhost:8080"

def run_login_tests():
    """运行登录功能测试"""
    try:
        logging.info("开始运行登录功能测试...")
        logging.info(f"使用BASE_URL: {config.BASE_URL}")
        
        # 创建报告目录
        report_dir = os.path.join(ROOT_DIR, "tests", "reports")
        os.makedirs(report_dir, exist_ok=True)
        
        # 设置报告文件路径
        report_file = os.path.join(report_dir, f"login_test_report_{timestamp}.html")
        
        # 运行测试
        pytest_args = [
            "-v",
            "--html=" + report_file,
            "--self-contained-html",
            os.path.join(ROOT_DIR, "tests", "test_cases", "test_login.py")
        ]
        
        result = pytest.main(pytest_args)
        
        # 使用print而不是logging输出最终结果，避免写入已关闭的日志
        if result == 0:
            print(f"所有登录测试用例通过！详细报告请查看: {report_file}")
        else:
            print(f"部分登录测试用例失败！详细报告请查看: {report_file}")
        
        return result
    except Exception as e:
        print(f"测试运行过程中发生错误: {e}")
        return 1

if __name__ == "__main__":
    run_login_tests() 